package com.example.myprojectoffeixue;

import android.app.Activity;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.localization.Gateway;
import com.localization.GatewayRequestUtil;
import com.localization.SendUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

public class YaoKongQActivity extends Activity implements OnClickListener {
    //操作电视遥控器界面
    Map<Integer,String > map;
    private MyApplication m;
    private boolean flag1;
    HongWaiSB h;
    private controllerList c;
    private Vibrator vibrator;
    private ImageView bt0;
    private ImageView bt1;
    private ImageView bt2;
    private ImageView bt3;
    private ImageView bt4;
    private ImageView bt6;
    private ImageView bt7;
    private ImageView bt8;
    private ImageView bt9;
    private ImageView btashang;
    private ImageView btaxia;
    private ImageView btaqueren;
    private ImageView btafanhui;
    private ImageView btazuo;
    private ImageView btayou;
    private ImageView btapindaojia;
    private ImageView btapindaojian;
    private ImageView btayinliangjia;
    private ImageView btayinlianjian;
    private ImageView btajingyin;
    private ImageView btaatot;
    private ImageView btagg;
    private ImageView btadianyuan;
    private ImageView bacaidan;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yao_kong_qi);
        info();
    }

    private void info() {
        ImageView iv = (ImageView) findViewById(R.id.abaocun);
        iv.setVisibility(View.GONE);
        Intent it = getIntent();
        c = (controllerList) it.getSerializableExtra("controllerList");

        if (c.selfLearning == 1) {

            flag1 = true;

            map = new HashMap<Integer,String>();
            for (int i = 0; i < c.controllerDetail.size(); i++) {
                int codeValue = c.controllerDetail.get(i).button;
                String code = c.controllerDetail.get(i).originalCode;
                map.put(codeValue, code);


            }

        }

        LinearLayout iv1 = (LinearLayout) findViewById(R.id.iv_yaokongqi);
        iv1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();

            }
        });
        m = (MyApplication) getApplication();
        h = new HongWaiSB();
        h.cmd = "1012";
        h.extra = new Extra();
        h.status = "04";
        if (!flag1) {
            h.status = "01";
        }

            h.extra.type = c.kind;
            h.mac=m.mac_ykq;
            h.ieee = m.ieee_ykq;
            h.port = m.port_ykq;


        bt0 = (ImageView) findViewById(R.id.a0);
        bt0.setOnClickListener(this);
        bt1 = (ImageView) findViewById(R.id.a1);
        bt1.setOnClickListener(this);
        bt2 = (ImageView) findViewById(R.id.a2);
        bt2.setOnClickListener(this);
        bt3 = (ImageView) findViewById(R.id.a3);
        bt3.setOnClickListener(this);
        bt4 = (ImageView) findViewById(R.id.a4);
        bt4.setOnClickListener(this);
        ImageView bt5 = (ImageView) findViewById(R.id.a5);
        bt5.setOnClickListener(this);
        bt6 = (ImageView) findViewById(R.id.a6);
        bt6.setOnClickListener(this);
        bt7 = (ImageView) findViewById(R.id.a7);
        bt7.setOnClickListener(this);
        bt8 = (ImageView) findViewById(R.id.a8);
        bt8.setOnClickListener(this);
        bt9 = (ImageView) findViewById(R.id.a9);
        bt9.setOnClickListener(this);
        btashang = (ImageView) findViewById(R.id.ashang);
        btashang.setOnClickListener(this);
        btaxia = (ImageView) findViewById(R.id.axia);
        btaxia.setOnClickListener(this);
        btaqueren = (ImageView) findViewById(R.id.aqueren);
        btaqueren.setOnClickListener(this);
        btafanhui = (ImageView) findViewById(R.id.afanhui);
        btafanhui.setOnClickListener(this);
        btazuo = (ImageView) findViewById(R.id.azuo);
        btazuo.setOnClickListener(this);
        btayou = (ImageView) findViewById(R.id.ayou);
        btayou.setOnClickListener(this);
        btapindaojia = (ImageView) findViewById(R.id.apindaojia);
        btapindaojia.setOnClickListener(this);
        btapindaojian = (ImageView) findViewById(R.id.apindaojian);
        btapindaojian.setOnClickListener(this);
        btayinliangjia = (ImageView) findViewById(R.id.ayinliangjia);
        btayinliangjia.setOnClickListener(this);
        btayinlianjian = (ImageView) findViewById(R.id.ayiliangjian);
        btayinlianjian.setOnClickListener(this);
        btajingyin = (ImageView) findViewById(R.id.ajingyin);
        btajingyin.setOnClickListener(this);
        btaatot = (ImageView) findViewById(R.id.aatot);
        btaatot.setOnClickListener(this);
        btagg = (ImageView) findViewById(R.id.agg);
        btagg.setOnClickListener(this);
        btadianyuan = (ImageView) findViewById(R.id.adianyuan);
        btadianyuan.setOnClickListener(this);
        bacaidan = (ImageView) findViewById(R.id.acaidan);
        bacaidan.setOnClickListener(this);
        vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);

    }


    public void send(int i) {

        String l = "";
        if (flag1) {
            if (map.containsKey(i)) {
                h.extra.code = map.get(i);
                l = JSON.toJSONString(h);

                    if(!Gateway.MacSocke.containsKey(m.mac_ykq)){
                        if (m.flag3) {
                            m.c.send(l);
                        }else {

                            HttpConnection.showToast(getBaseContext(),"命令发送失败，请重试");

                        }

                    }else{
                        String cmd = GatewayRequestUtil.createInfraredForwardingStr3(m.ieee_ykq, m.port_ykq,map.get(i));
                        SendUtil.Mac = m.mac_ykq;
                        SendUtil.cmd = cmd;
                        SendUtil.hand.sendEmptyMessage(0);
                    }
                    vibrator.vibrate(new long[]{0, 300}, -1);
            } else {
                HttpConnection.showToast(getBaseContext(),"你还没有学习本键");

            }


        } else {
            h.extra.codeIndex = c.controllerDetail.get(0).num + "";
            h.extra.codeValue = "" + i;
            l = JSON.toJSONString(h);

                if(!Gateway.MacSocke.containsKey(m.mac_ykq)){
                    if (m.flag3) {
                    m.c.send(l);}else {

                        HttpConnection.showToast(getBaseContext(),"命令发送失败，请重试");

                    }

                    }else{
                    Log.d("内网发送命令","内网"+m.ieee_ykq+"--"+m.port_ykq+"--"+m.type+"--"+c.controllerDetail.get(0).num+"--"+h.extra.codeValue);
                    String cmd = GatewayRequestUtil.createInfraredForwardingStr(m.ieee_ykq, m.port_ykq,m.type,c.controllerDetail.get(0).num,h.extra.codeValue);
                    SendUtil.Mac = m.mac_ykq;
                    SendUtil.cmd = cmd;
                    SendUtil.hand.sendEmptyMessage(0);
                }

                vibrator.vibrate(new long[]{0, 300}, -1);

        }
    }




    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.a0:
                send(35);

                break;

            case R.id.a1:
                send(15);
                break;

            case R.id.a2:
                send(17);

                break;

            case R.id.a3:
                send(19);
                break;

            case R.id.a4:
                send(21);

                break;

            case R.id.a5:
                send(23);
                break;

            case R.id.a6:
                send(25);
                break;

            case R.id.a7:
                send(27);
                break;

            case R.id.a8:
                send(29);

                break;

            case R.id.a9:
                send(31);

                break;

            case R.id.acaidan:
                send(5);

                break;

            case R.id.adianyuan:
                send(11);

                break;

            case R.id.afanhui:
                send(39);
                break;

            case R.id.agg:
                send(33);

                break;

            case R.id.ajingyin:
                send(13);
                break;
            case R.id.apindaojia:
                send(3);
                break;
            case R.id.apindaojian:
                send(7);

                break;
            case R.id.ayinliangjia:
                send(9);
                break;
            case R.id.ayiliangjian:
                send(1);
                break;
            case R.id.ashang:
                send(43);
                break;
            case R.id.axia:
                send(49);

                break;
            case R.id.azuo:
                send(45);
                break;
            case R.id.ayou:
                send(47);

                break;
            case R.id.aqueren:
                send(41);
                break;

            case R.id.aatot:
                send(37);

                break;

        }

    }

}
